package model;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Camera Control Class, provides control in java to record a webcam stream
 * 
 * @author INTI3D
 *
 */
public class CamControl {
	private String vlcPath = "\"C:\\Program Files\\VideoLAN\\VLC\\vlc.exe\"";
	private String recordingPath = "WebContent\\Recordings\\"; //webapps/webfarm/recordings 
	private Process record = null;
	
	/**
	 * start recording
	 */
	public void record(){
		try {
			Calendar c = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-DD_HH-mm-ss");
			String filename = sdf.format(c.getTime()) + ".mp4";
			System.out.println("saving recording as: " + recordingPath + filename);
			record = Runtime.getRuntime().exec(vlcPath + " --one-instance http://127.0.0.1:1337/stream.wmv :http-caching=1200 :sout=#transcode{vcodec=h264,vb=0,scale=0,acodec=mp4a,ab=128,channels=2,samplerate=44100}:file{dst=" + recordingPath + filename + "} :no-sout-rtp-sap :no-sout-standard-sap :ttl=1 :sout-keep");
			//TODO: variable stream destination, resolution, source, encoding
		} catch (IOException e) {
			//TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * stop recording
	 */
	public void stopRecord(){
		try {
			Runtime.getRuntime().exec(vlcPath + " --one-instance vlc://quit");
			Thread.sleep(1000);
			record.destroy();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
